package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.ExpertAnswerAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ExpertResultModel;
import com.banshuibao.bsb_android.model.PersonPageModelTwo;
import com.banshuibao.bsb_android.model.PersonalModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.view.MyListView;

import java.util.List;

public class PersonalPageActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private ImageView headImage;
    private FrameLayout consultation;
    private TextView name, duty_tv, consult_bt;
    private TextView officeName;
    private TextView className;
    private RadioGroup radioGroup;
    private TextView user_info;
    private MyListView listView;
    private ExpertAnswerAdapter adapter;
    private TextView money;
    private String uuid;
    private ExpertResultModel.DataBean dataBean;
    private List<PersonPageModelTwo.DataBean> dataBeans;
    private String userRole;
    private String userUuid;
    private String officeId;
    private GetLoginInfo getLoginInfo;
    private double advisoryPrice;
    private String cookieId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_page);
        adapter = new ExpertAnswerAdapter(this, false);
        getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
        userRole = getLoginInfo.getRole();
        userUuid = getLoginInfo.getUserId();
        officeId = getLoginInfo.getFirmId();
        cookieId = getLoginInfo.getCookieId();
        initView();
        getData();
        showProgressDialog();
        setPagePost();
    }

    private void getData() {
        //专家id
        uuid = getIntent().getStringExtra("uuid");
    }

    //专家个人主页
    private void setPagePost() {
        PersonalModel.body body = new PersonalModel.body();
        if (!TextUtils.isEmpty(uuid)) {
            body.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.PERSONAL_PAGE, body, new HttpResponse(this, PersonalModel.class) {
            @Override
            public void onResult(Object obj) {
                PersonalModel s = (PersonalModel) obj;
                PersonalModel.DataBean data = s.getData();
                setData(data);
                cancelProgressDialog();
            }
        });
    }

    private void setData(PersonalModel.DataBean data) {
        if (data != null) {
            String headImg = data.getHeadImg();
            String name1 = data.getName();
            //职责
            String duty = data.getDuty();
            //职位
            String positiotn = data.getPositiotn();
            String quote = data.getQuote();
            String skill = data.getSkill();
            String profile = data.getProfile();
            String role = data.getRole();
            String firmId = data.getFirmId();
            String firmName = data.getFirmName();
            String userId = data.getUuid();
            advisoryPrice = data.getAdvisoryPrice();
            String price = PriceUtil.FormatDecimal(advisoryPrice);
            consult_bt.setText("￥" + price + "/次 咨询");
            if (!TextUtils.isEmpty(firmName)) {
                officeName.setVisibility(View.VISIBLE);
                officeName.setText(firmName);
            } else {
                officeName.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(headImg)) {
                ImageUtil.loadImageHead(this, headImg, headImage);
            }
            if (!TextUtils.isEmpty(name1)) {
                name.setText(name1);
            }
            if (!TextUtils.isEmpty(duty)) {
                duty_tv.setText(duty);
            } else if (!TextUtils.isEmpty(positiotn)) {
                duty_tv.setText(positiotn);
            } else {
                duty_tv.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(skill)) {
                className.setText(skill);
            }
            if (!TextUtils.isEmpty(quote)) {
                money.setText(quote);
            }
            if (!TextUtils.isEmpty(profile)) {
                user_info.setText(profile);
            }
            if (TextUtils.isEmpty(cookieId)) {
                consult_bt.setEnabled(false);
                consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_one));
            }
            if (userRole.equals("2") || userRole.equals("3")) {
                if (role.equals("4")) {
                    consult_bt.setEnabled(true);
                    consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_two));
                } else {
                    consult_bt.setEnabled(false);
                    consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_one));
                }
            } else if (userRole.equals("0")) {
                if (role.equals("2") || role.equals("3")) {
                    if (officeId.equals(firmId)) {
                        consult_bt.setEnabled(true);
                        consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_two));
                    } else {
                        consult_bt.setEnabled(false);
                        consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_one));
                    }
                } else {
                    consult_bt.setEnabled(false);
                    consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_one));
                }
            } else if (userRole.equals("1")|| userRole.equals("4")) {
                if (role.equals("2") || role.equals("3") || role.equals("4")) {
                    consult_bt.setEnabled(true);
                    consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_two));
                } else {
                    consult_bt.setEnabled(false);
                    consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_one));
                }
            }
            if (userId.equals(userUuid)) {
                consult_bt.setEnabled(false);
                consult_bt.setBackground(getResources().getDrawable(R.drawable.consult_shape_one));
            }
        }
    }

    private void initView() {
        setTitleText("个人主页");
        headImage = findViewById(R.id.personal_head);
        name = findViewById(R.id.personal_name);
        className = (TextView) findViewById(R.id.personal_class_name);//擅长业务
        money = (TextView) findViewById(R.id.class_money);//收费标准
        duty_tv = findViewById(R.id.duty);
        consult_bt = findViewById(R.id.consult_bt);
        officeName = findViewById(R.id.office_name);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        consult_bt.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        user_info = (TextView) findViewById(R.id.personal_info);
        listView = (MyListView) findViewById(R.id.personal_listview);
        listView.setDivider(null);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (adapter != null && adapter.getCount() > 0) {
                    ExpertResultModel.DataBean dataBean = adapter.getItem(i);
                    //XToast.showToast(ExpertsAnswerActivity.this,"追问价格：+++"+dataBean.getAskPrice()+"元");
                    IntentManager.goExpertDetailActivity(PersonalPageActivity.this, dataBean.getUuid());
                }
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.title1:
                user_info.setVisibility(View.VISIBLE);
                listView.setVisibility(View.GONE);
                //user_info.setText("");
                break;
            case R.id.title2:
                user_info.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
                setQuestionPost();
                break;
            default:
                break;
        }
    }

    private void setQuestionPost() {
        PersonPageModelTwo.body body = new PersonPageModelTwo.body();
        body.setSinceId(1);
        body.setMaxId(10000);
        body.setType("home");
        if (!TextUtils.isEmpty(uuid)) {
            body.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.EXPERT_LIST, body, new HttpResponse(this, ExpertResultModel.class) {
            @Override
            public void onResult(Object obj) {
                ExpertResultModel s = (ExpertResultModel) obj;
//                LogUtil.e(s);
                List<ExpertResultModel.DataBean> data = s.getData();
                // adapter = new PersonalPageAdapter(PersonalPageActivity.this, data, R.layout.item_home_answer);
                adapter.setDownDate(data);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.consult_bt:
                IntentManager.goQuestionCloselyActivity(PersonalPageActivity.this, uuid, advisoryPrice, "o2o");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clear();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (adapter != null) {
            adapter.stopMedia();
        }
    }
}
